#include <iostream>
using std::cout;
using std::endl;

class Singleton
{
public:
    static Singleton * getInstance(){
        pthread_once(&_once, init);
        return _pInstance;
    }

private:

    static void init(){
        _pInstance = new Singleton();
         atexit(destroy);
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }

    Singleton(){
        cout << "Singleton()" << endl;
    }

    ~Singleton(){
        cout << "~Singleton()" << endl;
    }

private:
    static pthread_once_t _once;
    static Singleton * _pInstance;
};

Singleton * Singleton::_pInstance = nullptr;
pthread_once_t Singleton::_once = PTHREAD_ONCE_INIT;

void test(){
    Singleton * ps = Singleton::getInstance();
}

int main()
{
    test();
    return 0;
}
